/**
 * 
 */
package com.newtribe.security.cert;

import java.io.InputStream;
import java.rmi.RemoteException;
import java.security.cert.X509Certificate;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.digester.Digester;

import com.newtribe.rmi.rmiservice.ServiceBase;
import com.newtribe.security.remote.WorkerInstanceRemote;
import com.newtribe.security.util.FilePathUtil;

/**
 * @author newtribe
 *
 */
public class WorkerInstance extends ServiceBase implements WorkerInstanceRemote{

	public WorkerInstance()throws Exception{
		
	}
	private String id ;
	private KMCConfig kmcConfig ;
	private CAConfig caConfig ;
	private Set RAConfig =new HashSet();


	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#getId()
	 */
	public String getId() {
		return id;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#setId(java.lang.String)
	 */
	public void setId(String id) {
		this.id = id;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#getKmcConfig()
	 */
	public KMCConfig getKmcConfig() {
		return kmcConfig;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#setKmcConfig(com.newtribe.security.cert.KMCConfig)
	 */
	public void setKmcConfig(KMCConfig kmcConfig) {
		this.kmcConfig = kmcConfig;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#getCaConfig()
	 */
	public CAConfig getCaConfig() {
		return caConfig;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#setCaConfig(com.newtribe.security.cert.CAConfig)
	 */
	public void setCaConfig(CAConfig caConfig) {
		this.caConfig = caConfig;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#getRAConfig()
	 */
	public Set getRAConfig() {
		return RAConfig;
	}

	/* (non-Javadoc)
	 * @see com.newtribe.security.cert.WorkerInstanceRemote#setRAConfig(java.util.Set)
	 */
	public void setRAConfig(Set config) {
		RAConfig = config;
	}
	
	public String toString() {
		return "==========  id:"+this.id  ;
	}
	/**
	 * init the system config according xml config 
	 * user a Degest parser 
	 */
	public WorkerInstance init(InputStream in ) throws Exception {
		Object obj =this.getClass().getClassLoader();
		System.out.println(obj);
		Digester digester = new Digester();
		digester.setValidating(false);

		WorkerInstanceRemote instance = new WorkerInstance();


		digester.push(instance);
		//  digester.addObjectCreate("domain", DomainConfigMBean.class);
		digester.addSetProperties("instance");

		// Construct rule



		digester.addObjectCreate("instance/ca", null, CAConfig.class);
		digester.addSetProperties("instance/ca");
		digester.addBeanPropertySetter("instance/ca/listenAddress");
		digester.addBeanPropertySetter("instance/ca/listenPort");
		digester.addBeanPropertySetter("instance/ca/signkeyAlias");
		digester.addBeanPropertySetter("instance/ca/certAlias");

		digester.addBeanPropertySetter("instance/ca/certTemplate");
		

//		digester.setRules(new ExtendedBaseRules());
//		digester.addBeanPropertySetter("instance/ca/?");

		digester.addSetNext("instance/ca", "setCaConfig",
		"com.newtribe.security.cert.CAConfig");



		digester.addObjectCreate("instance/ca/kmc", null, KMCConfig.class);
		digester.addSetProperties("instance/ca/kmc");
		digester.addBeanPropertySetter("instance/ca/kmc/file");
		digester.addBeanPropertySetter("instance/ca/kmc/passwd");

		
		digester.addSetNext("instance/ca/kmc", "setKmcConfig",
		"com.newtribe.security.cert.KMCConfig");



		digester.addObjectCreate("instance/ca/ra", null, RAConfig.class);
		digester.addSetProperties("instance/ca/ra");
		digester.addBeanPropertySetter("instance/ca/ra/listenAddress");
		digester.addBeanPropertySetter("instance/ca/ra/listenPort");
		
		digester.addSetNext("instance/ca/ra", "addRAConfig",
		"com.newtribe.security.cert.RAConfig");

		WorkerInstance result =(WorkerInstance)digester.parse(in);

		return result;
		// Construct rule chain


	}
	
	public  byte[] signCert(String p10) throws  RemoteException {
		SignerEngine engine =new SignerEngine(this) ;
		
		System.out.println("===================process sign request \n" +p10) ;
		
		try {
			X509Certificate cert = engine.signCert(FilePathUtil.getUserDir() +caConfig.getCertTemplate() ,p10) ;
			
			byte[] derEncoded = cert.getEncoded();
			
			return derEncoded ;
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RemoteException(e.getMessage() ,e);
		}
	}
	
}
